package com.mediacloud.app.newsmodule.view.adapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzf.easyfloat.utils.DisplayUtils

import com.mediacloud.app.newsmodule.R

/**
 * @author Heaven Created on 2022/6/13
 */
class EmojiAdapter(context: Context) : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_emoji) {

    private var width = 0
    init {
        width = (DisplayUtils.getScreenWidth(context)-DisplayUtils.dp2px(context, 0f))/6

    }

    override fun convert(helper: BaseViewHolder, item: String?) {
        item?.let {
            val icEmoji = helper.getView<ImageView>(R.id.icEmoji)
            val params = icEmoji.layoutParams
            params.width = width
            params.height = width
            icEmoji.setPadding(0,20, 0, 20)
            try {
                if (item.startsWith("drawable/")){
                    val decodeStream = BitmapFactory.decodeStream(mContext.assets.open(item))
                    val drawable = BitmapDrawable(mContext.resources, decodeStream)
                    icEmoji.setImageDrawable(drawable)
                }else{
                    Glide.with(icEmoji.context).load(item).into(icEmoji)
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
//            val bitmap: Bitmap = if (item.startsWith("drawable/")) {
//                BitmapFactory.decodeStream(mContext.assets.open(item))
//            } else {
//                BitmapFactory.decodeFile(item)
//            }
//            val drawable = BitmapDrawable(mContext.resources, bitmap)
//            icEmoji.setImageDrawable(drawable)
        }

    }

    fun getHeight(): Int{
        return width
    }
}